package org.jeecg.modules.wlhy.customer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerConfig;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerConfigService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: hy_customer_config 货主参数设置
 * @Author: jeecg-boot
 * @Date: 2021-04-25
 * @Version: V1.0
 */
@Api(tags = "hy_customer_config")
@RestController
@RequestMapping("/customer/hyCustomerConfig")
@Slf4j
public class HyCustomerConfigController extends JeecgController<HyCustomerConfig, IHyCustomerConfigService> {
    @Autowired
    private IHyCustomerConfigService hyCustomerConfigService;

    @Autowired
    private UserUtil userUtil;


    /**
     * 分页列表查询
     *
     * @param hyCustomerConfig
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */

    @ApiOperation(value = "hy_customer_config-分页列表查询", notes = "hy_customer_config-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(HyCustomerConfig hyCustomerConfig,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<HyCustomerConfig> queryWrapper = QueryGenerator.initQueryWrapper(hyCustomerConfig, req.getParameterMap());



        Page<HyCustomerConfig> page = new Page<HyCustomerConfig>(pageNo, pageSize);
        IPage<HyCustomerConfig> pageList = hyCustomerConfigService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @GetMapping(value = "/getInfo")
    public Result<?> getInfo() {
        HyCustomer customer = userUtil.getCustomer();
        if (customer == null) {
            return Result.error("未找到货主信息");
        }
        HyCustomerConfig hyCustomerConfig = hyCustomerConfigService.getByCustomerId(customer.getId());
        if (hyCustomerConfig == null) {
            return Result.error("未找到货主配置信息");
        }
        return Result.OK(hyCustomerConfig);
    }

    /**
     * 添加
     *
     * @param hyCustomerConfig
     * @return
     */
    @AutoLog(value = "hy_customer_config-添加")
    @ApiOperation(value = "hy_customer_config-添加", notes = "hy_customer_config-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody HyCustomerConfig hyCustomerConfig) {

        HyCustomer customer;
        String customerId = null;
        if (userUtil.isSys()) {
            customerId = hyCustomerConfig.getCustomerId();
        } else {
            customer = userUtil.getCustomer();
            if (customer == null) {
                return Result.error("货主信息不存在!");
            }
            customerId = customer.getId();
            hyCustomerConfig.setCustomerId(customerId);
        }

        HyCustomerConfig config = hyCustomerConfigService.getByCId(customerId);
        if (config != null) {
            return Result.error("不能重复添加");
        }

        hyCustomerConfigService.save(hyCustomerConfig);
        return Result.OK("操作成功！");
    }

    /**
     * 编辑
     *
     * @param hyCustomerConfig
     * @return
     */
    @AutoLog(value = "hy_customer_config-编辑")
    @ApiOperation(value = "hy_customer_config-编辑", notes = "hy_customer_config-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HyCustomerConfig hyCustomerConfig) {
        if (!userUtil.isSys()) {
            HyCustomer customer = userUtil.getCustomer();
            if (customer == null) {
                return Result.error("货主信息不存在!");
            }
            if (!StringUtils.equals(customer.getId(), hyCustomerConfig.getCustomerId())) {
                return Result.error("货主信息错误");
            }
        }
        hyCustomerConfigService.edit(hyCustomerConfig);
        return Result.OK("编辑成功!");
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "hy_customer_config-通过id查询", notes = "hy_customer_config-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        HyCustomerConfig hyCustomerConfig = hyCustomerConfigService.getById(id);
        if (hyCustomerConfig == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(hyCustomerConfig);
    }

    /**
     * 通过货主id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "hy_customer_config-通过id查询", notes = "hy_customer_config-通过id查询")
    @GetMapping(value = "/queryByCustomerId")
    public Result<?> queryByCustomerId(@RequestParam(name = "id", required = true) String id) {
        return Result.OK(hyCustomerConfigService.getByCustomerId(id));
    }

//    /**
//    * 导出excel
//    *
//    * @param request
//    * @param hyCustomerConfig
//    */
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, HyCustomerConfig hyCustomerConfig) {
//        return super.exportXls(request, hyCustomerConfig, HyCustomerConfig.class, "hy_customer_config");
//    }
//
//    /**
//      * 通过excel导入数据
//    *
//    * @param request
//    * @param response
//    * @return
//    */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, HyCustomerConfig.class);
//    }

}
